/**
 * @author janet
 * @date 2015-5-4
 */
package net.oscorner.dao.menu;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import net.oscorner.commons.BaseDao;
import net.oscorner.entiry.Menus;


/**
 * @author janet
 * @date 2015-5-4 下午4:18:35
 * 
 */

public class MenuDaoImpl implements MenuDao{

	private BaseDao baseDao;
	private String [] qk = {"WHERE","AND"};
	public BaseDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}

	/* (non-Javadoc)
	 * @see net.oscorner.dao.menu.MenuDao#queryMenuList(net.oscorner.entiry.Menus)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Menus> queryMenuList(Menus entity) throws SQLException {
		// TODO Auto-generated method stub
		String strSql = " select * from t_menu ";
		List<String> params = new ArrayList<String>();
		List<Menus> list = new ArrayList<Menus>();
		
		int qk_idx = 0;
		if(entity.getMenuName() != null && !entity.getMenuName().trim().equals("")){
			strSql += qk[qk_idx] + " menuName like '%'||?||'%' ";
			params.add(entity.getMenuName());
			qk_idx =1;
		}
		if(entity.getType() != null && !entity.getType().trim().equals("")){
			strSql += qk[qk_idx] + " type = ? ";
			params.add(entity.getType());
			qk_idx = 1;
		}
		if(entity.getMenuPId() != null && !entity.getMenuPId().equals("")){
			strSql += qk[qk_idx] += " menuPid = ? ";
			params.add(entity.getMenuPId());
			qk_idx = 1;
		}
		list = baseDao.executeQuery(strSql, params.toArray(), entity.getClass());
		return list;
	}

	/* (non-Javadoc)
	 * @see net.oscorner.dao.menu.MenuDao#queryListCount(net.oscorner.entiry.Menus)
	 */
	@Override
	public int queryListCount(Menus entity) throws SQLException {
		// TODO Auto-generated method stub
		String sql = "select count(id) from t_menu";
		List<String> params = new ArrayList<String>();
		int result = baseDao.executeCount(sql, params.toArray());
		return result;
	}

	/* (non-Javadoc)
	 * @see net.oscorner.dao.menu.MenuDao#getMenu(net.oscorner.entiry.Menus)
	 */
	@Override
	public Menus getMenu(Menus e) throws SQLException {
		// TODO Auto-generated method stub
		String sql = "select * from t_menu where menuId = ? ";
		String [] param = {e.getMenuId()};
		Menus temp = (Menus)baseDao.executeQueryObject(sql, param, e.getClass());
		return temp;
	}

}
